import React, {Component} from "react";
import {Toast} from "antd-mobile";
import {restApiRequest} from "../Public/Api/Public";
import PropTypes from "prop-types";

export default class LoopViewPage extends Component {
    static contextTypes = {
        router: PropTypes.object
    }
    constructor(props, context) {
        super(props, context);
        this.state = {
            Ad: []//轮播图数组
        };
    }

    componentWillMount() {
        let param = {};
        let jsonData = restApiRequest.getSign(param);
        let guide_url = '/shop/api/getAdvertiselist';
        this.fetchGuideData(guide_url, jsonData);
    }

    //获取轮播图
    fetchGuideData(url, jsonData) {
        restApiRequest.fetchp(url, jsonData).then((response) => {
            if (!response.ok) {
                var error = new Error(response.statusText);
                error.response = response;
                throw error;
            } else {
                return response.json();
            }
        }).then((response_json) => {
            console.log(response_json);
            if (response_json.code === '0000') {
                let responseData = response_json.data;
                this.setState({
                    Ad: responseData
                })
            } else {
                return Toast.offline(response_json.msg, 2)
            }
        }).catch((ex) => {
            //ex.response.text().then((theText) => console.log(theText));
            console.log(ex);
        });
    }

    //轮播图点击事件
    onPress(data) {
        let ad_type = data.ad_type;
        if (ad_type == 1) {
            location.href = data.ad_url;
        } else if (ad_type == 0) {
            let product_url = '/shop/api/majorlist';
            let paramer = {major_product_id: data.major_product_id};
            let jsonData = restApiRequest.getSign(paramer);
            this.fetchData(product_url, jsonData);
        }
    }

    fetchData(url, jsonData) {
        restApiRequest.fetchp(url, jsonData).then((response) => {
            if (!response.ok) {
                var error = new Error(response.statusText);
                error.response = response;
                throw error;
            } else {
                return response.json();
            }
        }).then((response_json) => {
            let responseData = response_json.data;
            if (response_json.code === '0000') {
                localStorage.setItem('ProductDetail', JSON.stringify(responseData.major_product_list[0]))
                this.context.router.history.push({
                    pathname: '/ProductDetail',
                });
            }

        }).catch((ex) => {
            //ex.response.text().then((theText) => console.log(theText));
            console.log(ex);
        });
    }

    render() {

        return (
            <section className="home-banner-wrap">
                {this.state.Ad.map((data, i) => (
                    <img
                        key={i}
                        onClick={() => this.onPress(data)}
                        className="home-banner-img"
                        src={data.ad_pic}
                    />
                ))}
            </section>
        )
    }
}
